<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Request
 *
 * @author thuongnh
 */
class Request {

    //URI of the request
    var $uri;
    //Application Module. ie: default, admin, ...
    var $module;
    var $defaultModule = "default";
    //Controller
    var $controller;
    var $defaultController = "Index";
    var $defaultAction = "index";
    var $defaultUploadPath = "upload/";
    var $uploadPath;
    var $action;
    var $params;
    var $method;

    public function __construct() {
        $this->uri = $_SERVER['REQUEST_URI'];
        $this->method = $_SERVER['REQUEST_METHOD'];
        $this->module = "";
        $this->controller = "";
        $this->action = "";
        $this->uploadPath = Config::getConfig("path", "upload");
        if ($this->uploadPath == "") {
            $this->uploadPath = $this->defaultUploadPath;
        }
        $this->params = array();
        $this->parseURI();
    }

    public function isPost() {
        if (strcasecmp($this->method, "post") == 0) {
            return true;
        }
        return false;
    }

    protected function parseURI() {
        if ($this->uri != "") {
            $temp = ltrim($this->uri, "/");
            $uriParts = explode("/", $temp);
            Loader::loadClass("ModuleHelper");
            $modules = ModuleHelper::getModules();
            if (count($uriParts) >= 2) {
                $paramStart = 0;
                if (in_array($uriParts[0], $modules)) {
                    $this->module = $uriParts[0];
                    $controllers = ModuleHelper::getControllers($this->module);
                    if (in_array($uriParts[1], $controllers)) {
                        $this->controller = ucfirst($uriParts[1]);
                        if (isset($uriParts[2])) {
                            $this->action = $uriParts[2];
                            $paramStart = 3;
                        } else {
                            $this->action = $this->defaultAction;
                        }
                    } else {
                        $this->controller = $this->defaultController;
                    }
                } else {
                    $this->module = $this->defaultModule;
                    $this->controller = ucfirst($uriParts[0]);
                    $this->action = $uriParts[1];
                    $paramStart = 2;
                }

                if (strcasecmp($this->method, "GET") == 0) {
                    for ($i = $paramStart; $i < count($uriParts) - 1; $i+=2) {
                        if ($i == "") {
                            $i++;
                        }
                        $this->params[$uriParts[$i]] = $uriParts[$i + 1];
                    }
                } else if (strcasecmp($this->method, "POST") == 0) {
                    $this->params = $_POST;
                    if ($_FILES != null) {
                        foreach ($_FILES as $name => $info) {
                            if (is_uploaded_file($info['tmp_name'])) {
                                $fileLocation = $this->uploadPath . $info['name'];
                                if (move_uploaded_file($info['tmp_name'], $fileLocation)) {
                                    $this->params[$name] = $fileLocation;
                                } else {
                                    Logger::error("Cannot move uploaded file.");
                                }
                            }
                        }
                    }
                }
            } else if (count($uriParts) == 1) {
                if (in_array($uriParts[0], $modules)) {
                    $this->module = $uriParts[0];
                    $this->controller = ucfirst($this->defaultController);
                    $this->action = $this->defaultAction;
                } else {
                    $this->module = $this->defaultModule;
                    $this->controller = ucfirst($uriParts[0]);
                    $this->action = $this->defaultAction;
                }
            } else {
                $this->module = $this->defaultModule;
                $this->controller = ucfirst($this->defaultController);
                $this->action = $this->defaultAction;
            }
        }
    }

    public function getParam($key) {
        if (array_key_exists($key, $this->params)) {
            return $this->params[$key];
        }
        return null;
    }

    public function setParam($key, $value) {
        $this->params[$key] = $value;
    }

}

?>
